package grinch.edu.probtheory;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class BarGraph extends View{
	private LeastSquaresGraph currentPlot;
	private double delta = 0.05;
	private int absXInterval = 20;
	private int absXpx = 1200;
	private int absYpx = 417;
	private int colSteps = (int)(absXInterval/delta);
	private int oX = 20; 
	private int oY = 437;
	private int border = 20;
	private int koefY = 15;
	private int coorColor = Color.YELLOW;
	private int barColor = Color.GREEN;
	private int[] barVals;
	

	public BarGraph(Context context, LeastSquaresGraph plot) {
		super(context);
		if (!plot.equals(null)) currentPlot = plot;		
	}
	
	@Override
	protected void onDraw(Canvas c){
	    super.onDraw(c);
	    
	    Paint paint = new Paint();
	    paint.setStyle(Paint.Style.FILL);
	    
	    paint.setColor(coorColor);
	    c.drawLine(oX, border, oX, oY+20, paint); //coorY
	    for (int i=0;i<=(int)(absYpx/koefY);i++){
	    	int cY = oY - i*koefY;
	    	c.drawLine(oX+5, cY, oX-5, cY, paint);
	    	c.drawText(String.valueOf(i), oX-20, cY, paint);
	    }
	    
	    c.drawLine(20, 437, 1220, 437, paint);    //coorX
	    for (int i=0;i<=absXInterval;i++){
	    	int cX = border + i*absXpx/absXInterval;
	    	c.drawLine(cX, oY-5, cX, oY, paint);
	    	c.drawText(String.valueOf(i-10), cX, oY+20, paint);
	    }	    
	    
	    
	    paint.setColor(barColor);
	    currentPlot.updateValues();
	    barVals = new int[(int) (colSteps+1)];
	    
	    for (int i=0;i<currentPlot.getPlotState();i++){ 
	    	int pos = (int)(currentPlot.getRandomVal(i)/delta)+colSteps/2;
	    	if ((pos>=0)&&(pos<colSteps)) barVals[pos]++;
	    }
	    for (int i=0;i<=colSteps;i++){
	    	int cX = oX + absXpx/colSteps*i;
	    	int cY = oY - koefY*barVals[i];
	    	c.drawRect(cX-1, cY, cX+2, oY, paint);
	    }
	}
}
